package org.ragdollcat.secondaiagent.rag;

import org.ragdollcat.secondaiagent.third.baidu.transfer.BaiduTranslateResponse;
import org.ragdollcat.secondaiagent.third.baidu.transfer.TransApi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.ai.rag.Query;
import org.springframework.ai.rag.preretrieval.query.transformation.QueryTransformer;
import org.springframework.ai.rag.preretrieval.query.transformation.TranslationQueryTransformer;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;


public class BaiduTranslationQueryTransformer implements QueryTransformer {

    private static final Logger logger = LoggerFactory.getLogger(TranslationQueryTransformer.class);

    private final String text;

    private final String targetLanguage;

    private final String sourceLanguage;

    public BaiduTranslationQueryTransformer(String text, String targetLanguage) {
        this.sourceLanguage = "auto";
        this.targetLanguage = targetLanguage;
        this.text = text;
    }


    public Query doTransform(){
        return transform(null);
    }
    /**
     * 改造此方法，利用百度翻译的API进行翻译，而非调用大模型
     *
     * @param query The original query to transform
     * @return
     */
    @Override
    public Query transform(Query query) {
        TransApi transApi = new TransApi();
        BaiduTranslateResponse transResult = transApi.getTransResult(text, sourceLanguage, targetLanguage);

        String dst = transResult.getTransResult().get(0).getDst();
//        var translatedQueryText = this.chatClient.prompt()
//                .user(user -> user.text(this.promptTemplate.getTemplate())
//                        .param("targetLanguage", this.targetLanguage)
//                        .param("query", query.text()))
//                .options(ChatOptions.builder().temperature(0.0).build())
//                .call()
//                .content();

        if (!StringUtils.hasText(dst)) {
            logger.warn("Query translation result is null/empty. Returning the input query unchanged.");
            return query;
        }

        return new Query(text).mutate().text(dst).build();
    }


}
